package weiaibenpao.com.library.pedometer.presenter;


import weiaibenpao.com.library.pedometer.ApplicationModule;
import weiaibenpao.com.library.pedometer.bean.PedometerCardEntity;
import weiaibenpao.com.library.pedometer.listener.IGetPedometerResult;
import weiaibenpao.com.library.pedometer.listener.ISensorDataChanged;
import weiaibenpao.com.library.pedometer.model.PedometerRepository;
import weiaibenpao.com.library.pedometer.view.IPedometerView;


/**
 * Created by zoubo
 * 计步器presenter
 */
public class PedometerPresenter implements Presenter,ISensorDataChanged {
    private IPedometerView mView;
    private PedometerRepository mPedometerRepository;

    public PedometerPresenter(IPedometerView view) {
//        BaseApplication.globalRegisterEvent(this);
        mView = view;
        mPedometerRepository = ApplicationModule.getInstance().getPedometerRepository();
        ApplicationModule.getInstance().getPedometerRepository().registerOnSensorDataChangedListener(this);
    }

    @Override
    public void resume() {
        //第一次初始化数据:包括退出应用后再进入的数据初始化
        getPedometerStep();
    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
//        BaseApplication.globalUnRegisterEvent(this);
        ApplicationModule.getInstance().getPedometerRepository().unRegisterOnSensorDataChangedListener();
    }

    /**
     * 监听数据
     */
//    @Subscribe
//    public void onEventMainThread(PedometerEvent event) {
//        if (event.mIsUpdate) {
//            getPedometerStep();
//        }
//    }

    /**
     * 获取计步器的数据
     */
    public void getPedometerStep() {
        mPedometerRepository.getPedometerStep(new IGetPedometerResult() {
            @Override
            public void onSuccessGet(PedometerCardEntity cardEntity) {
                if (mView != null) {
                    mView.onReaderPedometer(cardEntity);
                }
            }

        });
    }

    @Override
    public void onSensorDataChanged() {
        getPedometerStep();
    }
}
